/**
 * Created by PanJiaChen on 16/11/18.
 */

/**
 * Parse the time to string
 * @param {(Object|string|number)} time
 * @param {string} cFormat
 * @returns {string | null}
 */
export function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
      time = parseInt(time)
    }
    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
    const value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
    return value.toString().padStart(2, '0')
  })
  return time_str
}

/**
 * @param {number} time
 * @param {string} option
 * @returns {string}
 */
export function formatTime(time, option) {
  if (('' + time).length === 10) {
    time = parseInt(time) * 1000
  } else {
    time = +time
  }
  const d = new Date(time)
  const now = Date.now()

  const diff = (now - d) / 1000

  if (diff < 30) {
    return '刚刚'
  } else if (diff < 3600) {
    // less 1 hour
    return Math.ceil(diff / 60) + '分钟前'
  } else if (diff < 3600 * 24) {
    return Math.ceil(diff / 3600) + '小时前'
  } else if (diff < 3600 * 24 * 2) {
    return '1天前'
  }
  if (option) {
    return parseTime(time, option)
  } else {
    return (
      d.getMonth() +
      1 +
      '月' +
      d.getDate() +
      '日' +
      d.getHours() +
      '时' +
      d.getMinutes() +
      '分'
    )
  }
}

/**
 * @param {string} url
 * @returns {Object}
 */
export function param2Obj(url) {
  const search = url.split('?')[1]
  if (!search) {
    return {}
  }
  return JSON.parse(
    '{"' +
      decodeURIComponent(search)
        .replace(/"/g, '\\"')
        .replace(/&/g, '","')
        .replace(/=/g, '":"')
        .replace(/\+/g, ' ') +
      '"}'
  )
}

/**
 * @param {Object} obj
 * @return {String}
 */
export function obj2Params(obj) {
  const str = []
  for (const p in obj) {
    str.push(
      encodeURIComponent(p) +
      '=' +
      encodeURIComponent(typeof obj[p] === 'object' ? JSON.stringify(obj[p]) : obj[p])
    )
  }
  return str.join('&')
}

/**
 * @param arr 枚举数组
 * @param key 枚举中作对比的字段名称（一般为唯一值）
 * @param rowValue 单元格数值
 * @param dataName 匹配枚举中的返回字段名称
 * @returns {string}
 */
export function formatColumnMap(arr, key, rowValue, dataName) {
  if (!arr || arr.length === 0) return ''
  let formatValue = '其他'
  if (arr.length > 0) {
    const match = arr.find(item => item[key] === rowValue)
    if (match) formatValue = match[dataName]
  }
  return formatValue
}
const list = {
  mobile: '^(0|86|17951)?(13[0-9]|15[0-9]|17[0-9]|18[0-9]|14[0-9]|19[0-9])[0-9]{8}$',
  wildmobile: '^(0|86|17951)?(13[0-9]|15[0-9]|17[0-9]|18[0-9]|14[0-9]|19[0-9])[0-9]{8}-[0-9]{1,2}$',
  tel: '^(\\d{3,4}-?)?\\d{6,8}$',
  wildtel: '^(\\d{3,4}-?)?\\d{6,8}-[0-9]{1,2}$',
  num: '^\\d*$',
  sfzh: /^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/
}
export function isWildPhone(d) {
  return isMobile(d) || isTel(d) || isWildMobile(d) || isWildTel(d)
}

function isMobile(d) {
  return isMatch(list.mobile, d)
}

function isMatch(r, s) { return new RegExp(r).test(s) }
function isTel(d) {
  return isMatch(this.list.tel, d)
}
function isWildMobile(d) {
  return isMatch(this.list.wildmobile, d)
}
function isWildTel(d) {
  return isMatch(this.list.wildtel, d)
}

export function isSfzh(d) {
  return isMatch(this.list.sfzh, d)
}
